package com.example.dessert.controller.fore;

import com.example.dessert.controller.BaseController;
import com.example.dessert.entity.User;
import com.example.dessert.service.UserService;
import com.example.dessert.util.ResponsePojo;
import com.example.dessert.util.ResultCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @ClassName: RegisterController
 * @Description: 注册
 * @Author li
 * @Date 2021/12/28
 * @Version 1.0
 */
@Controller
@RequestMapping("/fore")
public class RegisterController extends BaseController {

    @Resource
    private UserService userService;



    /**
     * 注册
     */
    @GetMapping("/register")
    public String register() {

        return "fore/register";

    }

    /**
     * 注册校验
     */
    @PostMapping("/doRegister")
    @ResponseBody
    public ResponsePojo doRegister(@RequestParam Map<String, Object> map) {
        logger.info("User registration request parameters :{}", map);

        String account = (String)map.get("account");
        User user = userService.accountQueryUser(account);
        logger.info("Account query user results :{}", user);

        if (user != null) {
            return ResponsePojo.error(ResultCode.EXP_EXIST_ACCOUNT,"用户名已存在，请重新输入！");
        }

        String password = (String)map.get("password");
        User users = new User();
        users.setAccount(account)
             .setPassword(password)
             .setNickname(account);
        userService.storeUser(users);

        return ResponsePojo.ok();

    }
}
